var _regions = new Array()
_regions[0] = "ef_region_inqu";
_regions[1] = "ef_region_result";
_regions[2] = "ef_region_solveadd";
_regions[3] = "ef_region_penaltyadd";
_regions[4] = "ef_region_mainadd";
var isNoSlove = true; // 标记是否为直接罚款
efform_onload = function() {
	InitRegionShow();
};   

function InitRegionShow() {
	hideAllRegions(_regions);
	$('#ef_region_inqu').show();
	$('#ef_region_result').show();
	efgrid.submitInqu("ef_grid_r", "ZM", "ZMCC11", "query");
}
//QUERY 查询
button_query_onclick = function()
{
	efgrid.submitInqu("ef_grid_r", "ZM", "ZMCC11", "query");
}; 
/*************************************************************************
*************************************************************************
	下达整改通知 方法 张坤2016年3月30日17:21:39
*************************************************************************
*************************************************************************/
//HANDLE 下达整改通知
button_handle_onclick = function()
{
	var grid = isSelectedOne("ef_grid_r");
	if (grid == 0)
		return;
	var selectedRow = grid.getSelectRowsData()[0];
	if (selectedRow["fdProblemStatus"] != 1) {
		SCZXAlertError("只能对未处理状态的记录进行该操作！");
		return;
	}

	// 默认赋值
	$('#solve_add-0-fdRecordId').val(selectedRow["fdKeyId"]);
	$('#solve_add-0-fdHandlePeople').val($('#fdCreator').val());
	$('#solve_add-0-fdHandlePeople').attr("readonly", "readonly");
	$('#solve_add-0-fdHandlePeopleId').val($('#fdCreatorId').val());
	$('#solve_add-0-fdCreateTime').val(getNowDateTime());
	$('#solve_add-0-fdHandleDate').val(getNowDate());
	$('#solve_add-0-fdStatus').val(1);
	$('#solve_add-0-fdBusinessDepartmentId').val(selectedRow["fdBusinessDepartmentId"]);
	hideAllRegions(_regions);
	$('#ef_region_solveadd').show();
}; 
// 下达整改通知页面 确定
button_solveconfirm_onclick = function()// HANDLE 主表 确定
{
	var isCheck = checkHandle();
	if (isCheck == 0) {
		return;
	}
	if (efvalidateDiv("ef_region_solveadd")) {

		if ($('#solve_add-0-fdIsPenalty').val() == "否") {
			var info = getDetailbyBlockname("ef_region_solveadd", "solve_add");
			if (null != info) {
				EiCommunicator.send("ZMCC11", "slove", info, ajax_solveconfirm_callback1);
			}
		} else {
			isNoSlove = false;
			// 初始化罚款页面
			$('#penalty_add-0-fdPenalty').val(1);
			$('#penalty_add-0-fdSendnoteDate').val(getNowDate());
			$('#penalty_add-0-fdSendnoteDepartmentId').val($('#fdCreateDepartmentId').val());
			$('#penalty_add-0-fdDepartmentId').val($('#solve_add-0-fdBusinessDepartmentId').val());
			// $('#solve_add-0-fdBusinessDepartmentId').val($('#result_add-0-fdBusinessDepartmentId').val());
			$('#penalty_add-0-fdSendnotePersonId').val($('#fdCreatorId').val());
			$('#penalty_add-0-fdSendnotePerson').val($('#fdCreator').val());

			// $('#penalty_add-0-fdRecordId').val(eiInfo.get("fdRecordId"));
			// $('#penalty_add-0-fdHandleId').val(eiInfo.get("fdHandleId"));
			$('#penalty_add-0-fdSendnoteDepartmentId').attr("disabled","disabled");
			$('#penalty_add-0-fdDepartmentId').attr("disabled", "disabled");
			hideAllRegions(_regions);
			$('#ef_region_penaltyadd').show();
		}
	}
}; 
//下达整改通知页面 确定 回调
var ajax_solveconfirm_callback1 = {
	onSuccess : function(eiInfo) {
		var grid = efgrid.getGridObject("ef_grid_r");
		grid.refresh(eiInfo);// 刷新主表grid
		InitRegionShow();
		clearAfterSolve("ef_region_solveadd");
	},
	onFail : function(eMsg) {
		SCZXAlertError("操作失败，原因：" + eMsg);
	}
}; 
//下达整改通知页面 返回
button_solvecancel_onclick = function ()//HANDLE 主表 返回
{
	InitRegionShow();
} 


/*************************************************************************
*************************************************************************
	罚款方法 张坤2016年3月30日17:21:39
*************************************************************************
*************************************************************************/
button_penalty_onclick = function ()//PENALTY 罚款 
{
	var grid = isSelectedOne("ef_grid_r");
	if (grid == 0)
		return;
	var selectedRow = grid.getSelectRowsData()[0]; 
    $('#ef_region_penaltyadd').find("input,select,textarea").each(function ()
    {
    	$(this).val('');
    	$('#penalty_add-0-fdSendnoteDepartmentId').attr("disabled","disabled");
//    	$('#penalty_add-0-fdDepartmentId').attr("disabled","disabled");
    })
	//初始化罚款页面
	$('#penalty_add-0-fdPenalty').val(1);
	$('#penalty_add-0-fdSendnoteDate').val(getNowDate());
	$('#penalty_add-0-fdSendnoteDepartmentId').val($('#fdCreateDepartmentId').val());
	$('#penalty_add-0-fdSendnotePersonId').val($('#fdCreatorId').val());
	$('#penalty_add-0-fdSendnotePerson').val($('#fdCreator').val());
	$('#penalty_add-0-fdRecordId').val(selectedRow['fdKeyId']);
	$('#penalty_add-0-fdDepartmentId').val(selectedRow['fdBusinessDepartmentId']);
	// departmentChanged();
	hideAllRegions(_regions);
	$('#ef_region_penaltyadd').show(); 
};  
// 罚款 确定
button_penaltyconfirm_onclick = function()// PENALTY 罚款 确定
{
	var isCheck = checkPenalty();
	if (isCheck == 0) {
		return;
	}

	if (efvalidateDiv("ef_region_penaltyadd")) {
		if (isNoSlove == false) {
			var info = getDetailbyBlockname("ef_region_solveadd", "solve_add");
			if (null != info) {
				EiCommunicator.send("ZMCC11", "slove", info, ajax_solveconfirm_callback2); // 下达整改通知
			}
		} else {
			var info = getDetailbyBlockname("ef_region_penaltyadd", "penalty_add");
			if (null != info) {
				EiCommunicator.send("ZMCC11", "penalty", info, ajax_penaltyconfirm_callback);// 直接罚款
			}
		}
	}
}; 
// 下达整改通知、罚款后 回调
var ajax_solveconfirm_callback2 = {
	onSuccess : function(eiInfo) {
		clearAfterSolve("ef_region_solveadd");
		$('#penalty_add-0-fdRecordId').val(eiInfo.get("fdRecordId"));
		$('#penalty_add-0-fdHandleId').val(eiInfo.get("fdHandleId"));
		var info = getDetailbyBlockname("ef_region_penaltyadd", "penalty_add");
		if (null != info) {
			EiCommunicator.send("ZMCC11", "penalty", info, ajax_penaltyconfirm_callback);// 罚款
		}
	},
	onFail : function(eMsg) {
		SCZXAlertError("操作失败，原因：" + eMsg);
	}
};
//下达整改通知、罚款后 回调
var ajax_penaltyconfirm_callback = {
	onSuccess : function(eiInfo) {
		var grid = efgrid.getGridObject("ef_grid_r");
		grid.refresh(eiInfo);// 刷新主表grid
		InitRegionShow();
		clearAfterSolve("ef_region_penaltyadd");
		isNoSlove = true;
	},
	onFail : function(eMsg) {
		SCZXAlertError("操作失败，原因：" + eMsg);
	}
};
// 罚款页面 点击 返回 按钮
button_penaltycancel_onclick = function()// PENALTY 罚款 返回
{
	InitRegionShow();
	clearAfterSolve("ef_region_penaltyadd");
	clearAfterSolve("ef_region_solveadd");
}; 

/*************************************************************************
*************************************************************************
	延期方法 张坤 2016年3月31日15:44:50
*************************************************************************
*************************************************************************/
button_delay_onclick = function ()//DELAY 延期 
{
	var grid=isSelectedOne("ef_grid_r");
	if (grid == 0)return; 
	var selectedRow=grid.getSelectRowsData()[0]; 
	for(var item in selectedRow){
		var colname=item;
		var colvalue=selectedRow[item];
		$('#result_add-0-'+colname).val(colvalue);
    	if(!colname.indexOf("fdDeadlineTimeF")>=0)
        {
    		$('#result_add-0-'+colname).attr("readonly","readonly");
    		$('#result_add-0-'+colname).attr("disabled","disabled");
        }
	}
	$('#result_add-0-fdProblemTimeF').attr("readonly","readonly");
	$('#result_add-0-fdProblemTimeF').attr("disabled","disabled");
	hideAllRegions(_regions);
	$('#ef_region_mainadd').show(); 
}; 



button_mainconfirm_onclick = function()// INSERT 主表 确定
{
	 var isCheck = checkDelay();
	 if (isCheck == 0) {
	 return;
	 }

	if (efvalidateDiv("ef_region_mainadd")) {
		var info = getDetail("ef_region_mainadd", "result_add");
		if (null != info) {
			EiCommunicator.send("ZMCC11", "delay", info, ajax_mainconfirm_callback);
		}
	}
}; 

var ajax_mainconfirm_callback = {
	onSuccess : function(eiInfo) {
		var grid = efgrid.getGridObject("ef_grid_r");
		grid.refresh(eiInfo);// 刷新主表grid
		InitRegionShow();
	},
	onFail : function(eMsg) {
		SCZXAlertError("操作失败，原因：" + eMsg);
	}
};
button_maincancel_onclick = function()// INSERT 主表 返回
{
	InitRegionShow();
};
/*************************************************************************
*************************************************************************
	新增方法 张坤2016年3月30日17:21:39
*************************************************************************
*************************************************************************/
button_insert_onclick = function() {
	var grid = isSelected("ef_grid_r");
	if (grid == 0)
		return;

	var rows = grid.getCheckedRows();
	for (var i = 0; i < rows.length; i++) {
		var fdProblemTimeF = grid.getCellValueByColumnName(rows[i], "fdProblemTimeF");
		var fdDeadlineTimeF = grid.getCellValueByColumnName(rows[i], "fdDeadlineTimeF");
		if (getDateValue(fdProblemTimeF) > getDateValue(fdDeadlineTimeF)) {
			EFAlert("发生时间不能晚于解决时间，请重新输入！");
			return;
		}
	}
	efgrid.submitForm("ef_grid_r", "ZM", "ZMCC11", "insert", true);
};
/*************************************************************************
*************************************************************************
	修改方法 张坤2016年3月30日17:21:39
*************************************************************************
*************************************************************************/
button_update_onclick = function() {
	var grid = isSelected("ef_grid_r");
	if (grid == 0)
		return;

	var rows = grid.getCheckedRows();
	for (var i = 0; i < rows.length; i++) {
		if (grid.getCellValueByColumnName(rows[i], "fdProblemStatus") != "1") {
			EFAlert("只能对未处理的记录进行修改！");
			return;
		}

		var fdProblemTimeF = grid.getCellValueByColumnName(rows[i], "fdProblemTimeF");
		var fdDeadlineTimeF = grid.getCellValueByColumnName(rows[i], "fdDeadlineTimeF");
		if (getDateValue(fdProblemTimeF) > getDateValue(fdDeadlineTimeF)) {
			EFAlert("发生时间不能晚于解决时间，请重新输入！");
			return;
		}
	}
	efgrid.submitForm("ef_grid_r", "ZM", "ZMCC11", "update", true);
};
/*************************************************************************
*************************************************************************
	作废 关闭 打印 方法 张坤2016年3月30日17:21:39
*************************************************************************
*************************************************************************/
button_invalid_onclick = function()// INVALID 作废
{
	var grid = isSelected("ef_grid_r");
	if (grid == 0)
		return;

	efgrid.submitForm("ef_grid_r", "ZM", "ZMCC11", "invalid", true);
};
button_close_onclick = function()// CLOSE 关闭
{
	var grid = isSelected("ef_grid_r");
	if (grid == 0)
		return;

	efgrid.submitForm("ef_grid_r", "ZM", "ZMCC11", "close", true);
};

button_print_onclick = function()// PRINT 打印
{
};  

/*************************************************************************
*************************************************************************
	其他方法 张坤2016年3月30日17:21:39
*************************************************************************
*************************************************************************/
//加载主表状态字段的颜色
efgrid_onCellDisplayReady = function( div_node_html, row_index, col_index, col_value_c, display_value ,grid_id)
{	
   var grid_obj = efgrid.getGridObject(grid_id);
   var columnEname = grid_obj.getColumn(col_index, TYPE_DATA).getEname();
   if( columnEname == "fdProblemStatus"  &&  grid_id=="ef_grid_r")
	{  
	 	var isNewLine = grid_obj.isNewLine(row_index);
        if(!isNewLine)
        {
            var html = "";
            if (col_value_c == "1") html = "<div align=center><font color='blue'>"+display_value+"</font></div>";   
            else if (col_value_c == "2") html = "<div align=center><font color='green'>"+display_value+"</font></div>";   
            else if (col_value_c == "3") html = "<div align=center><font color='red'>"+display_value+"</font></div>";   
            else if (col_value_c == "4") html = "<div align=center><font color='grey'>"+display_value+"</font></div>";   
            else if (col_value_c == "5") html = "<div align=center><font color='orange'>"+display_value+"</font></div>";   
            else html = "<div align=center>"+display_value+"</div>";   
            return html; 
        }else
        {	
            return;
        }
	}	
};
/**
 * 新增行后操作
 */
efgrid_afterAddNewRow = function(grid_id) {
	var grid = efgrid.getGridObject(grid_id);
	var rows = grid.getCheckedRows();
	if (grid_id == "ef_grid_r") {
		var fdCreateDepartmentId = $('#fdCreateDepartmentId').val();
		var fdCreator = $('#fdCreator').val();
		var time = getNowDateTime();// 当前时间

		for (var i = 0; i < rows.length; i++) {
			grid.getBlockData().setCell(rows[i], "fdKeyId", "");
			grid.getBlockData().setCell(rows[i], "fdProblemStatus", "1");// 状态
			grid.getBlockData().setCell(rows[i], "fdSubmitorDepartmentId", fdCreateDepartmentId);// 提出部门
			grid.getBlockData().setCell(rows[i], "fdSubmitor", fdCreator);// 提出人
			grid.getBlockData().setCell(rows[i], "fdProblemTimeF", time);// 发生时间
			grid.getBlockData().setCell(rows[i], "fdDeadlineTimeF", time);// 解决时间
		}
	}
};
/*
 * 生产问题延期——输入项数据校验
 */
function checkDelay() {
	var fdProblemTimeF = $("#result_add-0-fdProblemTimeF").val(); // 发生时间
	var fdDeadlineTimeF = $("#result_add-0-fdDeadlineTimeF").val(); // 解决时间

	if (fdDeadlineTimeF == "" || fdDeadlineTimeF == 0) {
		SCZXAlert("解决时间不能为空！");
		return false;
	} else {
		if (getDateValue(fdProblemTimeF) > getDateValue(fdDeadlineTimeF)) {
			SCZXAlert("发生时间不能晚于解决时间，请重新输入！");
			return false;
		}
	}
	return true;
};
/**
 * 处理——输入项校验
 */
function checkHandle() {
	var fdSuggestion = $("#solve_add-0-fdSuggestion").val().trim(); // 处理意见

	if (fdSuggestion == "") {
		SCZXAlert("处理意见不能为空！");
		return false;
	} else if (fdSuggestion.getBytes() > 500) {
		SCZXAlert("处理意见不能超过500个字符！");
		return false;
	}
	return true;
};

/**
 * 罚款——输入项校验
 */
function checkPenalty() {
	var fdPenalty = $("#penalty_add-0-fdPenalty").val(); // 罚款金额
	var fdPeopleName = $("#penalty_add-0-fdPeopleName").val().trim(); // 责任人
	var fdReason = $("#penalty_add-0-fdReason").val().trim(); // 罚款原因
	var fdRemarks = $("#penalty_add-0-fdRemarks").val().trim(); // 备注

	if (!IsNum(fdPenalty)) {
		SCZXAlert("罚款金额应输入长度不超过6位的正整数！");
		return false;
	}

	if (fdPeopleName == "") {
		SCZXAlert("责任人不能为空！");
		return false;
	} else if (fdPeopleName.getBytes() > 10 || fdPeopleName.getBytes() < 1) {
		SCZXAlert("[责任人]请输入1~10个字符！");
		return false;
	}

	if (fdReason == "") {
		SCZXAlert("罚款原因不能为空！");
		return false;
	} else if (fdReason.getBytes() > 250 || fdReason.getBytes() < 1) {
		SCZXAlert("[罚款原因]请输入1~250个字符！");
		return false;
	}

	if (fdRemarks.getBytes() > 100) {
		SCZXAlert("备注不能超过100个字符！");
		return false;
	}

	return true;
};

String.prototype.getBytes = function() {
	var cArr = this.match(/[^\x00-\xff]/ig);
	return this.length + (cArr == null ? 0 : cArr.length);
};  
String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g, "");
};
/*
根据传入的String类型的date获取该日期对应的毫秒值，传入的字符串格式为：yyyy-MM-dd HH:mm:ss
*/
function getDateValue(dateStr) {
   var tt = dateStr.split(" ");
   var ttt1 = tt[0].split("-");
   var ttt2 = tt[1].split(":");
   var date1 = new Date(ttt1[0], ttt1[1], ttt1[2], ttt2[0], ttt2[1], ttt2[2]);
   var m1 = date1.getTime();
   return m1;
}

/*
判断一个变量是不是不超过6位的正整数
*/
function IsNum(num){
var reNum=/^[1-9]\d{0,5}?$/;
return(reNum.test(num));
};

/**
 * 操作完成后清空该region内的各数据项的值
 */
var clearAfterSolve = function(region) {
	var temp;
	// 遍历INPUT
	$('#' + region).find("input").each(function() {
		temp = $(this).attr("id");
		$('#' + temp).val("");
	});
	// 遍历SELECT
	$('#' + region).find("select").each(function() {
		temp = $(this).attr("id");
		$('#' + temp).val("");
	});
	// 遍历TEXTAREA
	$('#' + region).find("textarea").each(function() {
		temp = $(this).attr("id");
		$('#' + temp).val("");
	});
};